package com.tencent.wxcloudrun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.dao.MealDao;
import com.tencent.wxcloudrun.domain.Meal;
import com.tencent.wxcloudrun.service.MealService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MealServiceImpl extends ServiceImpl<MealDao, Meal> implements MealService {

    @Autowired
    private MealDao mealDao;

    @Override
    @Transactional
    public Boolean update(Meal meal) {
        return mealDao.updateById(meal) > 0;
    }

    @Override
    @Transactional
    public Boolean delete(Long id) {
        return mealDao.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public Boolean add(Meal meal) {
        return mealDao.insert(meal) > 0;
    }

    @Override
    public Meal getById(Long id) {
        return mealDao.selectById(id);
    }

    @Override
    public List<Meal> getAll() {
        return mealDao.selectList(null);
    }

    @Override
    public IPage<Meal> getPage(int currentPage, int pageSize, Meal meal) {
        LambdaQueryWrapper<Meal> lqw = new LambdaQueryWrapper<Meal>();
        lqw.like(Strings.isNotEmpty(meal.getMealName()), Meal::getMealName, meal.getMealName());
        lqw.eq(meal.getMealKind() != null,Meal::getMealKind,meal.getMealKind());
        IPage<Meal> page = new Page<Meal>(currentPage,pageSize);
        mealDao.selectPage(page,lqw);
        return page;
    }
}
